package com.example.thirdhomework

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val FIRST_MSG = "FIRST_MSG"
const val SECOND_MSG = "SECOND_MSG"
class MainActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        Log.d("life cycle","onCreate")
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val first = findViewById<Button>(R.id.first)
        first.setOnClickListener {
            val intent = Intent(this, MainActivity2::class.java)
            val editText = findViewById<EditText>(R.id.editText)
            intent.putExtra("FIRST_MSG", editText.text.toString())
            startActivity(intent)
        }
        val second= findViewById<Button>(R.id.second)
        second.setOnClickListener {
            val intent = Intent(this, MainActivity3::class.java)
//            val editText = findViewById<EditText>(R.id.editText)
//            intent.putExtra("FIRST_MSG", editText.text.toString())
            startActivityForResult(intent,0)
        }//反向传参；request code为请求码
//        activity5
        val fourth = findViewById<Button>(R.id.fourth)
        fourth.setOnClickListener {
            val intent = Intent(this, MainActivity5::class.java)
            startActivity(intent)
        }
//        activity4
        val third = findViewById<Button>(R.id.third)
        third.setOnClickListener {
            val intent = Intent(this, MainActivity4::class.java)
            startActivity(intent)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0 && resultCode == 0) {
            val textView = findViewById<TextView>(R.id.textView)
            textView.text = data?.getStringExtra(SECOND_MSG)
        }
    }
}